/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.factory;

import static br.gov.cemaden.factory.ManagmentFactoryOption.CLASS_LOADING_MXBEAN_NAME;
import static br.gov.cemaden.factory.ManagmentFactoryOption.MEMORY_MXBEAN_NAME;
import static br.gov.cemaden.factory.ManagmentFactoryOption.OPERATING_SYSTEM_MXBEAN_NAME;
import static br.gov.cemaden.factory.ManagmentFactoryOption.THREAD_MXBEAN_NAME;
import com.sun.management.OperatingSystemMXBean;
import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.PlatformManagedObject;
import java.lang.management.ThreadMXBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;

/**
 *
 * @author Simone Santos
 */
public class MXBeanFactory {

    MBeanServerConnection connection;
    PlatformManagedObject plataform;

    public MXBeanFactory(MBeanServerConnection conn) {
        this.connection = conn;
    }

    public <T> T get(ManagmentFactoryOption option) {
        try {
            switch (option) {

                case THREAD_MXBEAN_NAME:
                    plataform = ManagementFactory.newPlatformMXBeanProxy(
                            connection, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
                    break;

                case MEMORY_MXBEAN_NAME:
                    plataform = ManagementFactory.newPlatformMXBeanProxy(
                            connection, ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
                    break;

                case CLASS_LOADING_MXBEAN_NAME:
                    plataform = ManagementFactory.newPlatformMXBeanProxy(
                            connection, ManagementFactory.CLASS_LOADING_MXBEAN_NAME, ClassLoadingMXBean.class);
                    break;

                case OPERATING_SYSTEM_MXBEAN_NAME:
                    plataform = ManagementFactory.newPlatformMXBeanProxy(
                            connection, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
                    break;
            }
        } catch (IOException ex) {
            Logger.getLogger(MXBeanFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (T) plataform;
    }
}
